package com.kh.rationaldrugreview.dto;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

import java.math.BigDecimal;
import java.util.List;

/**
 * 处方风险评估请求
 * Prescription Risk Assessment Request
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PrescriptionRiskAssessmentRequest {
    
    /**
     * 患者基本信息
     */
    private PatientInfo patient;
    
    /**
     * 处方药品列表
     */
    private List<PrescriptionDrug> drugs;
    
    /**
     * 诊断信息
     */
    private List<String> diagnoses;
    
    /**
     * 处方信息
     */
    private PrescriptionInfo prescriptionInfo;
    
    /**
     * 评估选项
     */
    private AssessmentOptions options;
    
    /**
     * 患者信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class PatientInfo {
        private String patientId;
        private String name;
        private String gender;              // 性别：男/女
        private Integer age;                // 年龄
        private Double weight;              // 体重(kg)
        private Double height;              // 身高(cm)
        
        // 妊娠哺乳信息
        private Boolean isPregnant;         // 是否怀孕
        private String pregnancyTrimester;  // 孕期：孕早期/孕中期/孕晚期
        private Integer gestationalWeek;    // 孕周
        private Boolean isLactating;        // 是否哺乳
        
        // 器官功能
        private String renalFunction;       // 肾功能：正常/轻度不全/中度不全/重度不全
        private Double creatinineClearance; // 肌酐清除率
        private String hepaticFunction;     // 肝功能：正常/轻度不全/中度不全/重度不全
        
        // 病史和过敏史
        private List<String> medicalHistory;   // 既往病史
        private List<String> allergies;        // 过敏史
        private List<String> currentMedications; // 当前用药
        
        // 生活方式
        private String smokingStatus;       // 吸烟状态
        private String drinkingStatus;      // 饮酒状态
        
        // 特殊状态
        private Boolean hasDiabetes;        // 糖尿病
        private Boolean hasHypertension;    // 高血压
        private Boolean hasHeartDisease;    // 心脏病
    }
    
    /**
     * 处方药品信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class PrescriptionDrug {
        private String drugName;            // 药品名称
        
        private String genericName;         // 通用名
        private String drugCode;            // 药品编码
        private String specification;       // 规格
        private String dosageForm;          // 剂型
        
        // 用法用量
        private BigDecimal dosage;          // 单次剂量
        private String dosageUnit;          // 剂量单位
        private String frequency;           // 用药频率
        private String administration;      // 给药途径
        private Integer duration;           // 疗程(天)
        private String usage;               // 用法
        
        // 药品属性
        private String category;            // 药品分类
        private String activeIngredient;    // 活性成分
        private String manufacturer;        // 生产厂家
        
        // 处方信息
        private String indication;          // 适应症
        private String prescriptionReason;  // 开处方原因
        private Boolean isEmergency;        // 是否急诊用药
    }
    
    /**
     * 处方信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class PrescriptionInfo {
        private String prescriptionId;
        private String prescriptionNumber;
        private String doctorName;
        private String department;
        private String prescriptionDate;
        private String prescriptionType;    // 处方类型：门诊/急诊/住院
        private List<String> diagnoses;     // 诊断
        private String remarks;             // 备注
    }
    
    /**
     * 评估选项
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class AssessmentOptions {
        private Boolean checkInteractions;     // 检查药物相互作用
        private Boolean checkContraindications; // 检查禁忌症
        private Boolean checkPregnancy;        // 检查妊娠风险
        private Boolean checkAge;             // 检查年龄适用性
        private Boolean checkOrganFunction;   // 检查器官功能
        private Boolean checkAllergies;       // 检查过敏风险
        private Boolean checkDuplication;     // 检查重复用药
        private Boolean generateRecommendations; // 生成建议
        
        private String assessmentLevel;       // 评估级别：基础/标准/全面
        private Boolean includeMinorRisks;    // 包含轻微风险
        private Boolean prioritizeByRisk;     // 按风险等级排序
        
        // 默认开启所有检查
        public static AssessmentOptions createDefault() {
            return AssessmentOptions.builder()
                .checkInteractions(true)
                .checkContraindications(true)
                .checkPregnancy(true)
                .checkAge(true)
                .checkOrganFunction(true)
                .checkAllergies(true)
                .checkDuplication(true)
                .generateRecommendations(true)
                .assessmentLevel("标准")
                .includeMinorRisks(true)
                .prioritizeByRisk(true)
                .build();
        }
    }
    
    /**
     * 获取评估选项，如果为空则使用默认选项
     */
    public AssessmentOptions getOptionsOrDefault() {
        return options != null ? options : AssessmentOptions.createDefault();
    }
    
    /**
     * 获取药品名称列表
     */
    public List<String> getDrugNames() {
        return drugs.stream()
            .map(PrescriptionDrug::getDrugName)
            .toList();
    }
    
    /**
     * 获取患者风险特征摘要
     */
    public String getPatientRiskSummary() {
        StringBuilder summary = new StringBuilder();
        
        if (patient.getIsPregnant() != null && patient.getIsPregnant()) {
            summary.append("孕妇 ");
        }
        
        if (patient.getIsLactating() != null && patient.getIsLactating()) {
            summary.append("哺乳期 ");
        }
        
        if (patient.getAge() != null && patient.getAge() < 18) {
            summary.append("儿童 ");
        } else if (patient.getAge() != null && patient.getAge() >= 65) {
            summary.append("老年人 ");
        }
        
        if (patient.getRenalFunction() != null && 
            patient.getRenalFunction().contains("不全")) {
            summary.append("肾功能不全 ");
        }
        
        if (patient.getHepaticFunction() != null && 
            patient.getHepaticFunction().contains("不全")) {
            summary.append("肝功能不全 ");
        }
        
        return summary.toString().trim();
    }
}